package net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpEchoServer {
    private DatagramSocket socket=null;
    public UdpEchoServer(int port) throws SocketException {
        socket=new DatagramSocket(port);
    }

    public void start() throws IOException {
        while (true){
            DatagramPacket requstPacket=new DatagramPacket(new byte[4399],4399);

            socket.receive(requstPacket);
            String requst=new String(requstPacket.getData(),0,requstPacket.getLength());
            String response=process(requst);
            DatagramPacket responsePacket=new DatagramPacket(response.getBytes(),response.getBytes().length,
                    requstPacket.getSocketAddress());
            socket.send(responsePacket);
        }
    }

    private String process(String requst) {
        return requst;
    }

    public static void main(String[] args) throws IOException {
        UdpEchoServer server=new UdpEchoServer(9090);
        server.start();
    }
}
